package com.wasion.pwportable.jsi;

import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.webkit.WebView;

import com.google.gson.Gson;
import com.wasion.core.db.Pagination;
import com.wasion.core.db.storage.SqliteStorage;
import com.wasion.core.db.storage.SqliteStorageListener.DataInsertListener;
import com.wasion.core.db.storage.SqliteStorageListener.DataQueryListener;
import com.wasion.core.db.storage.StorageQuery;
import com.wasion.core.db.storage.StorageQuery.SortOrder;
import com.wasion.core.util.StringUtil;
import com.wasion.pwportable.activity.PwPortableActivity;
import com.wasion.pwportable.dao.sd.LogSDDBDao;
import com.wasion.pwportable.domain.po.Log;

/**
 * 
 * @ClassName: LogJSI
 * @Description: 日志JavaScriptInterface，对日志操作交互接口<br/>
 *               1. 增加日志 <br/>
 *               2. 获取日志 <br/>
 * @author 高意
 * @date 2014-8-1 下午2:57:06
 * 
 */
public class LogJSI {

	// 应用上下文
	private Context context;

	// webview，浏览器窗口
	private WebView webView;

	// Sqlite存储实例
	private SqliteStorage sqliteStorage = null;

	// Google的Json转换工具
	private Gson gson = new Gson();

	// 日志数据库访问接口
	private LogSDDBDao logSDDBDao = null;

	public LogJSI(Context context) {
		this.context = context;
		this.webView = PwPortableActivity.getWebView();
		this.sqliteStorage = SqliteStorage.getInstance(this.context);
		logSDDBDao = new LogSDDBDao(context);
	}

	/**
	 * 
	 * @Title: addLog
	 * @Description: 新增日志
	 * @param
	 * @return
	 * @throws
	 */
	public void addLog(final String jsonStr) {

		Log log = new Log();
		log = gson.fromJson(jsonStr, Log.class);

		sqliteStorage.insertData(log, logSDDBDao, new DataInsertListener() {

			public void onSuccess(long paramLong) {
				// JSIHelper.showToast(context, "添加成功!");
				// Map<String, Object> map = new HashMap<String, Object>();
				// map.put("type", "log");

				// String result = gson.toJson(map);
				// webView.loadUrl("javascript:editSuccessCallback(" + result
				// + ")");
			}

			public void onFailure(int errorCode, String errorMessage) {
				// JSIHelper.showToast(context, "添加失败!");
			}
		});
	}

	/**
	 * 
	 * @Title: queryLogsByPage
	 * @Description: 分页获取日志
	 * @param
	 * @return
	 * @throws
	 */
	public void queryLogsByPage(final String jsonStr) {

		StorageQuery storageQuery = new StorageQuery();
		try {
			JSONObject jsObj = new JSONObject(jsonStr);

			storageQuery.setOffset(0);
			if (!jsObj.isNull("pagesize")) {
				storageQuery.setLimit(Integer.parseInt(jsObj
						.getString("pagesize")));
			} else {
				storageQuery.setLimit(Pagination.DEFAULT_PAGE_SIZE);
			}

			if (!jsObj.isNull("terminalid")
					&& !StringUtil.isEmpty(jsObj.getString("terminalid"))) {
				storageQuery
						.equals("terminalid", jsObj.getString("terminalid"));
			} else {
				JSIHelper.showToast(context, "请选择终端!");
				return;
			}

			if (!jsObj.isNull("time") && 0 != jsObj.getLong("time")) {
				storageQuery.lessThan("time", jsObj.getLong("time"));
			}

			storageQuery.addSort("_id", SortOrder.DESC);
		} catch (JSONException e1) {
			e1.printStackTrace();
		}

		sqliteStorage.findData(storageQuery, logSDDBDao,
				new DataQueryListener() {

					public void onFailure(int errorCode, String errorMessage) {
						JSIHelper.showToast(context, "查询日志失败!");
					}

					public void onSuccess(List<?> paramList) {

						String jsonStr = gson.toJson(paramList);
						if (null != paramList && 0 < paramList.size()) {
							try {
								webView.loadUrl("javascript:queryLogsByPageCallback("
										+ jsonStr + ")");
							} catch (Exception e) {
								JSIHelper.showToast(context, e.getMessage());
							}
						} else {
							JSIHelper.showToast(context, "已加载所有日志!");
						}
					}
				});
	}

}
